<!--
 * @Descripttion: 查看数据
 * @Author: xiao li
 * @Date: 2021-01-27 14:33:01
 * @LastEditors: xiao li
 * @LastEditTime: 2021-09-10 15:50:59
-->
<template>
  <div class="lb-sys-sproconfig" v-loading="pageLoading">
    <top-nav :isBack="true"></top-nav>
    <div class="count-chart">
      <div class="flex-warp">
        <div class="flex-column">
          <div>浏览量</div>
          <div class="f-title c-title text-bold mt-sm">{{ info.pv }}</div>
        </div>
        <div class="flex-column">
          <div>回收量</div>
          <div class="f-title c-title text-bold mt-sm">{{ info.recovery }}</div>
        </div>
      </div>
      <div style="width: 1065px; height: 300px; background: white">
        <count-echarts :datas="date_list" />
      </div>
    </div>
    <div class="space-lg fill-body"></div>
    <div class="page-main">
      <div class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="提交时间" prop="range">
            <el-date-picker
              v-model="range"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              value-format="timestamp"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="用户昵称" prop="title">
            <el-input
              v-model="searchForm.title"
              placeholder="请输入用户昵称"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </div>

      <div class="page-search-form" style="padding-bottom: 24px">
        <el-row>
          <el-col :span="18" class="text-bold flex-y-center">数据列表</el-col>
          <el-col :span="6" class="text-right">
            <lb-button
              plain
              type="primary"
              icon="el-icon-download"
              :loading="downloadLoading"
              @click="toExportStaff"
              >{{ $t('action.export') }}</lb-button
            >
          </el-col>
        </el-row>
        <div class="fill-base space-lg"></div>
        <el-table
          ref="multipleTable"
          v-loading="loading"
          :data="tableData"
          :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
          tooltip-effect="dark"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="id" label="ID"></el-table-column>
          <el-table-column
            prop="create_time"
            label="问卷提交时间"
          ></el-table-column>
          <el-table-column prop="nickName" label="昵称"></el-table-column>
          <el-table-column prop="avatarUrl" label="头像">
            <template slot-scope="scope">
              <lb-image :src="scope.row.avatarUrl" />
            </template>
          </el-table-column>
          <el-table-column prop="id" label="操作">
            <template slot-scope="scope">
              <div class="table-operate">
                <lb-button
                  size="mini"
                  plain
                  type="primary"
                  @click="
                    $router.push(`/app/questionnaire/detail?id=${scope.row.id}`)
                  "
                  >{{ $t('action.viewData') }}</lb-button
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
        <lb-page
          :isShowBatch="false"
          :page="searchForm.page"
          :pageSize="searchForm.limit"
          :total="total"
          :selected="multipleSelection.length"
          @handleSizeChange="handleSizeChange"
          @handleCurrentChange="handleCurrentChange"
        >
        </lb-page>
      </div>
    </div>
  </div>
</template>

<script>
import pubic from '@/utils/public'
import countEcharts from './countEcharts'
export default {
  data () {
    return {
      pickerOptions: {
        disabledDate (time) {
          return time.getTime() > Date.now()
        }
      },
      range: '',
      searchForm: {
        id: 0,
        page: 1,
        limit: 10,
        start_time: '',
        end_time: '',
        title: ''
      },
      downloadLoading: false,
      multipleSelection: [],
      loading: false,
      info: {},
      tableData: [],
      total: 0
    }
  },
  components: {
    countEcharts
  },
  created () {
    let { id } = this.$route.query
    this.searchForm.id = id
    this.getTableDataList()
  },
  methods: {
    resetForm (name) {
      this.$refs[name].resetFields()
      this.range = ''
      this.getTableDataList(1)
    },
    async getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      let { range } = this
      let isChange = typeof (range) === 'object' && range && range.length > 0
      this.searchForm.start_time = isChange ? range[0] / 1000 : ''
      this.searchForm.end_time = isChange ? range[1] / 1000 + 24 * 3600 - 1 : ''
      let { code, data } = await this.$api.appointmentQueForm(this.searchForm)
      this.loading = false
      if (code !== 200) return
      this.info = data.info
      this.tableData = data.list.data
      this.total = data.list.total
      this.date_list = data.date_list
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    /**
       * @method 导出
       */
    toExportStaff () {
      this.downloadLoading = true
      let { id, title } = JSON.parse(JSON.stringify(this.searchForm))
      let { range } = this
      let multipleSelection = JSON.parse(JSON.stringify(this.multipleSelection))
      let searchForm = {
        que_id: id,
        id: [],
        start_time: '',
        end_time: '',
        title
      }
      if (typeof (range) === 'object' && range && range.length > 0) {
        searchForm.start_time = range[0] / 1000
        searchForm.end_time = range[1] / 1000 + 24 * 3600 - 1
      }
      if (typeof (multipleSelection) === 'object' && multipleSelection && multipleSelection.length > 0) {
        multipleSelection.map(item => {
          searchForm.id.push(item.id)
        })
      }
      let url = pubic.getProCurrentHref()
      let keywords = url.indexOf('?') > 0 ? '' : '?'
      let flag = url.indexOf('?') > 0
      Object.getOwnPropertyNames(searchForm).forEach((keys, value) => {
        keywords += flag ? `&${keys}=${searchForm[keys]}` : `${keys}=${searchForm[keys]}`
        flag = true
      })
      let token = sessionStorage.getItem('minitk')
      let dwonlaodUrl = `${url}/appoint/admin/AdminQuestion/queExcel${keywords}&token=${token}`
      window.location.href = dwonlaodUrl
      setTimeout(() => {
        this.downloadLoading = false
      }, 5000)
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}

</script>

<style lang="scss" scoped>
.flex-warp {
  display: flex;
  flex-wrap: wrap;
}

.flex-y-center {
  display: flex;
  align-items: center;
}
.count-item {
  width: 23.5%;
  height: 165px;
  background: white;
  margin-left: 2%;
  padding: 20px;
  .iconfont {
    color: #7094fa;
    font-size: 16px;
    margin-right: 10px;
  }
  .tool-tips {
    margin-top: 0px;
    padding-top: 0px;
    flex: 1;
    color: #666;
  }
  .manage-num {
    margin-top: 5px;
  }
}
.count-item:nth-child(1) {
  margin-left: 0;
}
.flex-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.count-manage {
  width: 30%;
  height: 320px;
  margin-right: 20px;
  background: white;
  padding: 20px 0 0 20px;
  .manage-title {
    font-size: 14px;
    color: #333;
    margin-top: 9px;
    margin-bottom: 50px;
  }
  .manage-item {
    width: 50%;
    height: 100px;
    display: inline-block;
    float: left;
  }
}
.manage-text {
  font-size: 13px;
  color: #999999;
}
.manage-num {
  font-size: 20px;
  color: #333;
}
.manage-today {
  font-size: 10px;
  color: #3388ff;
}

.count-chart {
  width: 68%;
  // height: 300px;
  background: white;
  margin-top: 30px;
  .flex-warp {
    .flex-column {
      margin-left: 50px;
      .f-title {
        font-size: 28px;
      }
    }
  }
}

.el-form {
  width: 100%;
}
.el-input,
.el-select {
  width: 200px;
}
.el-cascader {
  width: 300px;
}
.space-lg {
  width: 100%;
  height: 20px;
}
</style>
